#include "register.h"
#include "login.h"
#include "recv.h"
#include "send_water_pump.h"
#include "send_data.h"
#include <signal.h>
#include "sys_log.h"

typedef struct socke_type
{
	char soc[5];
}ST;


// 用户类型
typedef enum {
	USER = 2,
	ADMIN = 1,
	EXIT = 0
} UserType;

// 角色的操作
typedef enum {
	LOGIN = 1,
	REGISTER = 2
} UserAction;

//水表的操作
typedef enum {
	ADD_PUMP = 1,
	DELETE_PUMP = 2,
	SELECT_PUMP = 3
} PumpAction;

// 数据的操作
typedef enum {
	CURRENT_DATA = 1,
	HISTORICAL_DATA = 2
} DataAction;



void fun(int sig){
	if(sig == SIGUSR1)
	{
		printf("数据异常\n");

		printf("\a");
	}
}

int main(int argc, char *argv[])
{
	ST socke_type;
	int sock = 0;
	int ret = 0;
	STA dat;
	memset(&dat,0,sizeof(dat));
	WP pump;
	memset(&pump, 0, sizeof(pump));
	DAT detail;
	memset(&detail,0,sizeof(detail));

	// 初始化信号
	signal(SIGUSR1,fun);


	log_init();
	//连接网络
	struct sockaddr_in serAddr;
	memset(&serAddr, 0, sizeof(serAddr));
	serAddr.sin_family = AF_INET;
	serAddr.sin_port = htons(6500); 
	serAddr.sin_addr.s_addr = inet_addr("192.168.48.128");

	sock = socket(AF_INET, SOCK_STREAM, 0);
	if (sock == -1) {
		perror("sock");
		exit(-1);
	}

	ret = connect(sock, (struct sockaddr*)&serAddr, sizeof(serAddr));
	if (ret == -1) {
		perror("connect");
		exit(-1);
	}

	strcpy(socke_type.soc,"fd1");
	ret = send(sock,&socke_type,sizeof(ST),0);
	if (ret == -1){
		printf("send---ok");
	}

	// 'admin'-- 管理员 'user'-- 用户
	int choice;
	// 用户选择   1-- 'login'-- 登录     2-- 'register'-- 注册     0--退出
	int user_choice;
	// 管理员选择  1-- 'login'-- 登录     2-- 'register'-- 注册    0--退出
	int admin_choice;
	// 水泵选择   1-- 'select'-- 查看    2-- 'add'-- 添加   3-- 'delete'-- 删除    0--退出
	int pump_choice;
	// 水泵信息   'select'-- 查看
	int detail_id;

	// 用户结构体
	USE user_info;



	while (1) {
		printf("\t\t\t欢迎访问---井下水泵房在线检测系统\n");
		printf("\t\t\t请选择您的身份\n");
		printf("1:管理员\t2:用户\t0:退出\n");

		scanf("%d", &choice);


		// 管理员
		if (choice == ADMIN) {
			memset(&user_info, 0, sizeof(user_info));
			user_info.type = choice;
			while (1){
				printf("请选择你想要的功能:");
				printf("1:登录\t2:注册\t0:退出\n");
				// 登录
				scanf("%d", &admin_choice);
				if (admin_choice == LOGIN) {
					LOG_I("管理员登录");
					user_info.user_type = admin_choice;
					Login(&user_info,sock);//登录
					// 接受判断是否登陆成功
					dat = unpack(sock);
					if (dat.type == 1){
						printf("%s\n",dat.state);
						LOG_I("管理员登录成功");
						while (1){
							printf("请选择你想要的功能:");
							printf("1:新建水表\t2:删除水表\t3:查看水表\t0:退出\n");
							scanf("%d", &pump_choice);
							if (pump_choice == ADD_PUMP){
								memset(&pump, 0, sizeof(pump));
								// 对水表进行操作
								pump.type = 3;
								pump.pump_type = pump_choice;
								printf("请输入水表名称:");
								scanf("%s", pump.pump_name);
								printf("xxxxxx:%d\n",sock);
								send_water_pump(&pump,sock);
								dat = unpack(sock);
								if (dat.type == 1){
									printf("%s\n",dat.state);
								}
								else if (dat.type == 0){
									LOG_I("管理员登录失败");
									printf("%s\n",dat.state);
								}
							}
							else if (pump_choice == DELETE_PUMP){
								memset(&pump, 0, sizeof(pump));
								// 对水表进行操作
								pump.type = 3;
								pump.pump_type = pump_choice;
								printf("请输入水泵名称:");
								scanf("%s", pump.pump_name);
								LOG_I("管理员操作水泵");
								send_water_pump(&pump,sock);
								dat = unpack(sock);
								if (dat.type == 1){
									printf("%s\n",dat.state);
								}
								else if (dat.type == 0){
									printf("%s\n",dat.state);
								}
							}
							else if(pump_choice == SELECT_PUMP){
								memset(&pump, 0, sizeof(pump));
								pump.type = 3;
								pump.pump_type = pump_choice;
								send_water_pump(&pump,sock);

								printf("\t水泵数据\t\t\t\n");
								WP data;
								while(1) {
									memset(&data,0,sizeof(data));
									data= recv_water_pump(sock);

									if (data.id == 0) {
										break;
									}
									printf("id : %d\tname : %s\n",data.id,data.pump_name);
								}

								// 操作details

								detail.type = 4;
								// 查看当前数据

								while(1){
									printf("请输入你想查询水表的id:");
									scanf("%d",&detail.pump_id);
									printf("请选择你想要的功能:");
									printf("1:查询当前时间水表信息\t2:查询历史水表信息\t0:退出\n");
									scanf("%d",&detail.data_type);
									if (detail.data_type == 1){
										send_details(&detail,sock);
										detail = unpack_details(sock);
										printf("%s\n温度:%.2f℃\n液位:%.2fL\n流量计:%.4fS/cm\n电动球阀:%d\n负压传感器:%.2fPa\n压力传感器:%.2fPa",detail.time,detail.temperature,detail.liquid_level,detail.flow_rate,detail.valve_state,detail.negative_pressure,detail.pressure);
										if(detail.temperature > 10)
										{
											printf("温度过高");
											raise(SIGUSR1);
										}
										else if(detail.liquid_level > 60)
										{
											printf("温度过高");
											raise(SIGUSR1);
										}
									}
									if (detail.data_type == 2){
										send_details(&detail,sock);
										while(1){
											detail = unpack_details(sock);

											if(detail.pump_id  == 0){
												break;
											}

											printf("%d\n",detail.pump_id);
											printf("%s\n温度:%.2f℃\n液位:%.2fL\n流量计:%.4fS/cm\n电动球阀:%d\n负压传感器:%.2fPa\n压力传感器:%.2fPa",detail.time,detail.temperature,detail.liquid_level,detail.flow_rate,detail.valve_state,detail.negative_pressure,detail.pressure);
										}
									}
									if (detail.data_type == 0){
										break;
									}
								}
							}
							else if (pump_choice == EXIT){
								break;
							}
						}
					}
					// 登陆错误
					else if (dat.type == 0){
						printf("%s",dat.state);
					}
				}
				// 注册
				else if (admin_choice == REGISTER) {
					user_info.user_type = admin_choice;
					LOG_I("注册新管理员");
					Register(&user_info, sock);//注册
					// 接受判断是否注册成功
					dat = unpack(sock);
					// 注册成功
					if (dat.type == 1){
						printf("%s\n",dat.state);
					}
					// 注册错误
					else if (dat.type == 0){
						printf("%s\n",dat.state);
					}
				}
				// 退出
				else if (admin_choice == EXIT) {
					break;
				}


			}
		}
		
		// 用户
		if (choice == USER) {
			memset(&user_info, 0, sizeof(user_info));
			user_info.type = choice;
			while (1){
				printf("请选择你想要的功能:");
				printf("1:登录\t2:注册\t0:退出\n");

				scanf("%d", &admin_choice);
				// 登录
				if (admin_choice == LOGIN) {
					user_info.user_type = admin_choice;
					Login(&user_info,sock);//登录
					LOG_I("用户进行登录");
					// 接受判断是否登陆成功
					dat = unpack(sock);
					// 登陆成功
					if (dat.type == 1){
						printf("%s\n",dat.state);
						// 服务器端直接发送水泵名字，我接收
						memset(&pump, 0, sizeof(pump));
						printf("\t水泵数据\t\t\t\n");
						WP data;
						while(1) {
							memset(&data,0,sizeof(data));
							data= recv_water_pump(sock);
							if (data.id == 0) {
								break;
							}
							printf("id : %d\tname : %s\n",data.id,data.pump_name);
						}
						while(1){
							printf("请输入你想查看的水泵id:");
							scanf("%d",&detail.pump_id);
							printf("请选择你想要的功能:");
							printf("1:查询当前时间水表信息\t2:查询历史水表信息\t0:退出\n");
							LOG_I("用户查看水泵");
							scanf("%d",&detail.data_type);
							if (detail.data_type == 1){
								send_details(&detail,sock);
								detail = unpack_details(sock);
								printf("%s\n温度:%.2f℃\n液位:%.2fL\n流量计:%.4fS/cm\n电动球阀:%d\n负压传感器:%.2fPa\n压力传感器:%.2fPa",detail.time,detail.temperature,detail.liquid_level,detail.flow_rate,detail.valve_state,detail.negative_pressure,detail.pressure);
							}
							if (detail.data_type == 2){
								send_details(&detail,sock);
								while(1){
									detail = unpack_details(sock);
									if(detail.pump_id  == 0){
										break;
									}
									printf("%s\n温度:%.2f℃\n液位:%.2fL\n流量计:%.4fS/cm\n电动球阀:%d\n负压传感器:%.2fPa\n压力传感器:%.2fPa",detail.time,detail.temperature,detail.liquid_level,detail.flow_rate,detail.valve_state,detail.negative_pressure,detail.pressure);
								}
							}
							if (detail.data_type == 0){
								break;
							}
						}

					}
					else if (dat.type == 0){
						printf("%s",dat.state);
					}
				}
				// 注册
				else if (admin_choice == REGISTER) {
					user_info.user_type = admin_choice;
					Register(&user_info, sock);//注册
					// 接受判断是否注册成功
					dat = unpack(sock);
					if (dat.type == 1){
						printf("%s\n",dat.state);
						LOG_I("用户注册成功");
					}
					else if (dat.type == 0){
						printf("%s\n",dat.state);
						LOG_I("用户注册失败");
					}
				}

				else if (admin_choice == EXIT) {
					LOG_I("用户退出");
					break;
				}
			}
		}

		if (choice == 0) {
			break;
		}
		
	}
	log_destroy();
	unpack(sock);//关闭套接字
	return 0;
}

















